package com.example.musicplayer.service;

import com.example.musicplayer.service.IMusicServiceCallback;

import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;

public class MusicPlaybackUtils {

    private Context mAppContext;
    private MusicServiceProxy mServiceProxy = null;

    private static MusicPlaybackUtils sInstance = null;

    private MusicPlaybackUtils(Context context) {
        this.mAppContext = context.getApplicationContext();
    }

    public static MusicPlaybackUtils getInstance(Context context) {
        if (sInstance != null) {
            return sInstance;
        }

        synchronized (MusicPlaybackUtils.class) {
            if (sInstance == null) {
                sInstance = new MusicPlaybackUtils(context);
            }
        }

        return sInstance;
    }

    public void startService(Intent intent) {
        this.mAppContext.startService(intent);
    }

    public void bindService(Intent intent, int flags) {
        this.mServiceProxy = MusicServiceProxy.getInstance(this.mAppContext);

        this.mServiceProxy.connect(intent, flags);
    }

    public boolean isServiceConnected() {
        if (this.mServiceProxy != null && this.mServiceProxy.isConnected()) {
            return true;
        }

        return false;
    }

    public void open(String path) {
        if (isServiceConnected()) {
            try {
                this.mServiceProxy.open(path);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean isPlaying() {
        boolean result = false;
        if (isServiceConnected()) {
            try {
                result = this.mServiceProxy.isPlaying();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    public void pause() {
        if (isServiceConnected()) {
            try {
                this.mServiceProxy.pause();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    public void prev() {
        if (isServiceConnected()) {
            try {
                this.mServiceProxy.prev();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    public void next() {
        if (isServiceConnected()) {
            try {
                this.mServiceProxy.next();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    public void stop() {
        if (isServiceConnected()) {
            try {
                this.mServiceProxy.stop();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    public void play() {
        if (isServiceConnected()) {
            try {
                this.mServiceProxy.play();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    public long duration() {
        long result = 0L;
        if (isServiceConnected()) {
            try {
                result = this.mServiceProxy.duration();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    public long position() {
        long result = 0L;
        if (isServiceConnected()) {
            try {
                result = this.mServiceProxy.position();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    public void seek(long position) {
        if (isServiceConnected()) {
            try {
                this.mServiceProxy.seek(position);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    public void playList(long[] ids, int position) {
        if (isServiceConnected()) {
            try {
                this.mServiceProxy.playList(ids, position);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    public long[] getPlayingList() {
        long[] result = null;
        if (isServiceConnected()) {
            try {
                result = this.mServiceProxy.getPlayingList();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    public void setCallback(IMusicServiceCallback cb) {
        if (isServiceConnected()) {
            try {
                this.mServiceProxy.setCallback(cb);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    public void setLogging(int level) {
        if (isServiceConnected()) {
            try {
                this.mServiceProxy.setLogging(level);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }
}
